package jsjf;
import java.util.Arrays;
import jsjf.exceptions.*;

public class ArrayStack<T>  implements StackADT<T>
{
    private final int DEFAULT_CAPACITY =100;

    private int top;
    private T[] stack;
    @SuppressWarnings("uncheched")
    ArrayStack()
    {
        top =0;

        stack = (T[]) (new Object[DEFAULT_CAPACITY]);
    }
    @Override
    public void push(T element)
    {
        if(size() == stack.length) {
            expandCapacity();
        }
        stack[top] = element;
        top++;
    }
    private void expandCapacity()
    {
        stack = Arrays.copyOf(stack,stack.length*2);
    }
    @Override
    public int size( )
    {
        return top;
    }

    @Override
    public String toString() {
        String result1=" ";
        for(int a=0;a<top;a++){
            result1+=stack[a]+" ";
        }
        return result1;
    }
    @Override
    public T pop() throws EmptyCollectionException
    {
        if (isEmpty())
            throw new EmptyCollectionException("Stack");
       top--;


        T result=stack[top];
        stack[top] =null;

        return result;
    }

    @Override
    public T peek() throws EmptyCollectionException

    {
        if (isEmpty()) {
            throw new EmptyCollectionException("Stack");
        }
        return stack[top-1];

    }
    @Override
    public boolean isEmpty()
    {
        if (stack[0] == null) {
            return true;
        }
        else {
            return false;
        }
        }
}
